options("icpsr_email" = "dy212@georgetown.edu", "icpsr_password" = "Fbp9nbmKreLsubd8nL5H")Final Project
Reproducible data retrieval
crime
# takes 1-2 mins
icpsr_download(
file_id = 38649,
download_dir = here("data")
)
crime<-read_dta(here("data","ICPSR_38649","DS0001","38649-0001-Data.dta"))Clean data
crime
# crime data at 2013
crime_data<-crime|>
rename_all(tolower)|>
rename(county=stcofips)|>
mutate(viol = murder+rape+robbery+agasslt)|>
mutate(property = burglry+larceny+mvtheft)|>
select(county,year,viol,property)
attr(crime_data$viol, "label")<-"Total violent crimes reported (MURDER + RAPE + ROBBERY + AGASSLT)"
attr(crime_data$property, "label")<-"Total property crimes reported (BURGLRY + LARCENY + MVTHEFT)"# Extract state-level FIPS codes (first two digits of county FIPS codes)
crime_data$state_fips <-
substr(crime_data$county,
1, 2)
state_fips_to_abb <-
tibble(
state_fips = c("01", "02", "04", "05", "06",
"08", "09", "10", "11", "12",
"13", "15", "16", "17", "18",
"19", "20", "21", "22", "23",
"24", "25", "26", "27", "28",
"29", "30", "31", "32", "33",
"34", "35", "36", "37", "38",
"39", "40", "41", "42", "44",
"45", "46", "47", "48", "49",
"50", "51", "53", "54", "55", "56"),
state_abb = c("AL", "AK", "AZ", "AR", "CA", "CO",
"CT", "DE", "DC", "FL", "GA", "HI",
"ID", "IL", "IN", "IA", "KS", "KY",
"LA", "ME", "MD", "MA", "MI", "MN",
"MS", "MO", "MT", "NE", "NV", "NH",
"NJ", "NM", "NY", "NC", "ND", "OH",
"OK", "OR", "PA", "RI", "SC", "SD",
"TN", "TX", "UT", "VT", "VA", "WA",
"WV", "WI", "WY")
)Map
# Convert county-level data to state-level data, aggregate violent crime data
crime_data_map <- crime_data |>
group_by(state_fips) |>
summarise(
total_violence = sum(viol,
na.rm = TRUE),
total_property = sum(property,
na.rm = TRUE)
) |>
left_join(state_fips_to_abb,
by = 'state_fips') |>
mutate(
hover_violence = paste(state_abb,
'<br>',
"Total Violent Crimes: ",
total_violence),
hover_property = paste(state_abb,
'<br>',
"Total Property Crimes: ",
total_property)
) |>
pivot_longer(
cols = c(total_violence,
total_property),
names_to = "crime_type",
values_to = "crime_count"
)# Create and output the map for total violence
violence_map <-
plot_geo(crime_data_map |>
filter(crime_type == "total_violence"),
locationmode = 'USA-states') |>
add_trace(
z = ~crime_count,
text = ~hover_violence,
hoverinfo = 'text',
locations = ~state_abb,
color = ~crime_count,
colors = c("#1a9641", "#ffffbf", "#fdae61", "#d7191c"),
colorbar = list(title = "Total Violent Crimes (million)")
) |>
layout(
title = 'US Crimes by State, 2009 - 2014',
geo = list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
)
# Create and output the map for total property
property_map <-
plot_geo(crime_data_map |>
filter(crime_type == "total_property"),
locationmode = 'USA-states') |>
add_trace(
z = ~crime_count,
text = ~hover_property,
hoverinfo = 'text',
locations = ~state_abb,
color = ~crime_count,
colors = c("#2b83ba", "#ffffbf", "#fdae61", "#d7191c"),
colorbar = list(title = "Total Property Crimes (million)")
) |>
layout(
title = 'US Crimes by State, 2009 - 2014',
geo = list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
)
# Place the two maps side by side in a stacked layout
fig <- subplot(violence_map,
property_map,
nrows = 2,
margin = 0.05)
# Print the figure
fig